<?php
include 'base.php';
include 'require_member.php';
include 'connectdb.php';
$code = $_GET["code"];

function insert_bill($invoice_code, $product_code, $quantity, $customer_code, $seller_code, $discount, $date){
    $sql1 = "SELECT sale_price from product WHERE code = '".$product_code."'";
    $prices = mysql_fetch_array(mysql_query($sql1));
    $price = $prices['sale_price'];
    $total = ($price*$quantity*(100 - $discount))/100;
    
    $sql="INSERT INTO bill(invoice_code, product_code, price, quantity, customer_code, seller_code, discount, date, total)VALUES('$invoice_code','$product_code', '$price', '$quantity', '$customer_code', '$seller_code', '$discount', '$date', '$total')";    
    return mysql_query($sql);
}

if($_POST){    
    if (insert_bill($_POST[invoice_code], $_POST[product_code], $_POST[quantity], $_POST[customer_code], $_POST[seller_code], $_POST[discount], $_POST[date])){    
        $sql="UPDATE bill SET status='0' where code ='".$code."'";
        if (mysql_query($sql, $co))
            echo "Đã cập nhật thành công hóa đơn!";
    }
    else{                  
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500);
        }                    
}
?>

<script language="JavaScript" type="text/javascript">

    $(document).ready(function () {        
        $( "#date" ).datepicker();        
        $("#product_code").change(function(){            
            $.ajax({
                type: "post",
                url: "get_product_info.php",
                data: {product_code :  $('#product_code').val()},
                dataType: "json",
                success: function(data)
                {                     
                        $("#price").val(data.price);                       
                },
                error: function()
                {                     
                        alert('Có lỗi xảy ra, kiểm tra lại thông tin') ;
                }               

            }); 
            return false;
        });        
        $("#discount").change(function(){
            if ($(this).val() > 100){
                $(this).val(100);
            }

            if ($(this).val() < 0){
                $(this).val(0);
            }
        });

        $("#submit").click(function(){           
            if($("#product_code").val() == ''|| $("#quantity").val() == '' || $("#seller_code").val() == '' || $("#date").val() == '' || $("#discount").val() == ''){
                alert("Chưa điền đủ thông tin");
                return false;
            }

            var product_code = $('#product_code').val(),
                quantity = $('#quantity').val(),
                price = $('#price').val(),
                customer_code = $('#customer_code').val(),
                invoice_code = $('#invoice_code').val(),                            
                seller_code = $('#seller_code').val(),                            
                date = $('#date').val(),                            
                discount = $('#discount').val(),
                total = price*quantity*(100-discount)/100;                    
            $("#total").val(total);
            $.ajax({
                type: "post",
                url: "",
                data: "product_code=" + product_code + "&quantity=" + quantity + "&customer_code=" + customer_code + "&seller_code=" + seller_code + "&date=" + date +  "&discount=" + discount + "&invoice_code=" + invoice_code,
                success: function () {                    
                    alert("Đã cập nhật thành công");                    
                    window.location.href = $("#redirect").val();                    
                    
                },
                error: function() {                    
                    alert("Có lỗi xảy ra");
                }                
            }); 
            return false;

        });
        
    });
</script>

<?php

	$sSQL="SELECT * FROM bill where bill.code ='".$code."' and bill.status = '1'";
    $subresult=mysql_query($sSQL);
    if(mysql_num_rows($subresult) > 0){
        while($row = mysql_fetch_array($subresult))                                
            {                                	
        		echo '<div class="form_settings">';
        		echo '<p> <span>Mã</span>'.$row['code'].'</p>';
                echo '<input id = "invoice_code" type="hidden" value="'.$row['invoice_code'].'">';
                echo '<input id = "seller_code" type="hidden" value="'.$row['seller_code'].'">';
                echo '<input id = "customer_code" type="hidden" value="'.$row['customer_code'].'">';
                echo '<input id = "date" type="hidden" value="'.$row['date'].'">';
        		echo '<p> <span>Mã sản phẩm</span>'; ?>
                <select id = "product_code" name="product_code">                    
                    <?php
                    $stSQL="select code from product";
                    $resultsub=mysql_query($stSQL);           
                    if(mysql_num_rows($resultsub)> 0)            
                        while($row2 = mysql_fetch_array($resultsub))
                        if ($row2['code'] == $row['product_code']){
                        ?>          
                            <option selected="selected" value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                        <?php
                        }
                        else{
                            ?>
                            <option value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                            <?php
                        }         
                        ?>
                    </select></p>
                <?php
                echo '<p> <span>Giá</span> <input type="text" title="hệ thống sẽ tự cập nhật giá" disabled="disabled" name="price" id = "price" value="'.$row['price'].'"></p>';
        		echo '<p> <span>Số lượng</span> <input type="text" name="quantity" id = "quantity" value="'.$row['quantity'].'"></p>';        		              
        		echo '<p> <span>Giảm giá</span> <input type="text" name="discount" id = "discount" value="'.$row['discount'].'"></p>';                        		          		
                echo '<p> <span>Tổng</span> <input type="text" title = "Hệ thống sẽ tự cập nhật tổng tiền" name="total" id = "total" value="'.$row['total'].'"" disabled = "disabled" ></p>';
        		echo '<span>&nbsp</span><input class="submit" type="submit" id = "submit" name="update" value="Cập nhật" />';            
                if ($_COOKIE['name'] == 1)
                    echo '<input type="hidden" id="redirect" value="edit_hoa_don.php?code='.$row['invoice_code'].'">';            
                else
                    echo '<input type="hidden" id="redirect" value="hoa_don.php?code='.$row['invoice_code'].'">';            

		        echo '</div>';                             
            }            
    }
    else{
    	echo "Không có hóa đơn này hoặc hóa đơn đã được sửa. Vui lòng chọn hóa đơn khác";
    }
?>
<?php
include 'footer.php';
?>